package cn.chinasyq.photoquan.news.adapter;

import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.android.volley.toolbox.NetworkImageView;

import java.util.ArrayList;
import java.util.List;

import cn.chinasyq.photoquan.R;
import cn.chinasyq.photoquan.news.bean.ArticlesEntity;
import cn.master.volley.commons.VolleyHelper;

/**
 * Created by 一搏 on 2015/11/24.
 */
public class ArticlesListAdapter extends BaseAdapter {
    public List<ArticlesEntity> getArticlesEntities() {
        return articlesEntities;
    }

    public void setArticlesEntities(List<ArticlesEntity> articlesEntities) {
        this.articlesEntities = articlesEntities;
        notifyDataSetChanged();
    }

    public void addArticlesEntities(List<ArticlesEntity> articlesEntities) {
        this.articlesEntities.addAll(articlesEntities);
        notifyDataSetChanged();
    }

    private List<ArticlesEntity> articlesEntities;

    public ArticlesListAdapter() {
        articlesEntities = new ArrayList<>();
    }

    @Override
    public int getCount() {
        return articlesEntities.size();
    }

    @Override
    public Object getItem(int position) {
        return articlesEntities.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ArticlesEntity entity = (ArticlesEntity) getItem(position);
        if (entity.getImages() == null) {
            TextView textView = new TextView(parent.getContext());
            textView.setText(entity.getTitle());
            textView.setTextColor(parent.getResources().getColor(R.color.C3));
            textView.setTextSize(TypedValue.COMPLEX_UNIT_PX,parent.getResources().getDimensionPixelSize(R.dimen.T1));
            textView.setPadding(20,20,20,20);
            return textView;
        } else if (entity.getImages().size() > 1) {
            return getMultipleImageView(entity, position, convertView, parent);
        } else {
            return getOneImageView(entity, position, convertView, parent);
        }
    }


    private View getOneImageView(ArticlesEntity entity, int position, View convertView, ViewGroup parent) {
        ViewHolderOneImage viewHolder;
        if (convertView == null || convertView.getTag() == null || !(convertView.getTag() instanceof ViewHolderOneImage)) {
            viewHolder = new ViewHolderOneImage();
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_news_one_image, null);
            viewHolder.tv_title = (TextView) convertView.findViewById(R.id.tv_title);
            viewHolder.iv_image = (NetworkImageView) convertView.findViewById(R.id.iv_image);
            viewHolder.tv_from = (TextView) convertView.findViewById(R.id.tv_from);
            viewHolder.tv_collection_num = (TextView) convertView.findViewById(R.id.tv_collection_num);
            viewHolder.tv_comment_num = (TextView) convertView.findViewById(R.id.tv_comment_num);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolderOneImage) convertView.getTag();
        }
        viewHolder.tv_title.setText(entity.getTitle());
        viewHolder.tv_from.setText(entity.getAuthor());
        viewHolder.tv_collection_num.setText(String.valueOf(entity.getFavorites()));
        viewHolder.tv_comment_num.setText(String.valueOf(entity.getComments()));
        if (entity.getImages().size() == 0) {
            viewHolder.iv_image.setVisibility(View.GONE);
        } else {
            viewHolder.iv_image.setImageUrl(entity.getImages().get(0), VolleyHelper.getImageLoader());
        }
        return convertView;
    }

    private View getMultipleImageView(ArticlesEntity entity, int position, View convertView, ViewGroup parent) {
        ViewHolderMultiple viewHolder;
        if (convertView == null || convertView.getTag() == null || !(convertView.getTag() instanceof ViewHolderMultiple)) {
            viewHolder = new ViewHolderMultiple();
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_news_multiple_image, null);
            viewHolder.tv_title = (TextView) convertView.findViewById(R.id.tv_title);
            viewHolder.iv_image = (NetworkImageView) convertView.findViewById(R.id.iv_image);
            viewHolder.iv_image2 = (NetworkImageView) convertView.findViewById(R.id.iv_image2);
            viewHolder.iv_image3 = (NetworkImageView) convertView.findViewById(R.id.iv_image3);
            viewHolder.tv_from = (TextView) convertView.findViewById(R.id.tv_from);
            viewHolder.tv_collection_num = (TextView) convertView.findViewById(R.id.tv_collection_num);
            viewHolder.tv_comment_num = (TextView) convertView.findViewById(R.id.tv_comment_num);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolderMultiple) convertView.getTag();
        }
        viewHolder.tv_title.setText(entity.getTitle());
        viewHolder.tv_from.setText(entity.getAuthor());
        viewHolder.tv_collection_num.setText(String.valueOf(entity.getFavorites()));
        viewHolder.tv_comment_num.setText(String.valueOf(entity.getComments()));
        if (entity.getImages().size() > 0) {
            viewHolder.iv_image.setImageUrl(entity.getImages().get(0), VolleyHelper.getImageLoader());
        } else {
            viewHolder.iv_image.setVisibility(View.INVISIBLE);
        }
        if (entity.getImages().size() > 1) {
            viewHolder.iv_image2.setImageUrl(entity.getImages().get(1), VolleyHelper.getImageLoader());
        } else {
            viewHolder.iv_image2.setVisibility(View.INVISIBLE);
        }
        if (entity.getImages().size() > 2) {
            viewHolder.iv_image3.setImageUrl(entity.getImages().get(2), VolleyHelper.getImageLoader());
        } else {
            viewHolder.iv_image3.setVisibility(View.INVISIBLE);
        }
        return convertView;
    }

    class ViewHolderOneImage {
        TextView tv_title, tv_from, tv_comment_num, tv_collection_num;
        NetworkImageView iv_image;
    }

    class ViewHolderMultiple {
        TextView tv_title, tv_from, tv_comment_num, tv_collection_num;
        NetworkImageView iv_image, iv_image2, iv_image3;
    }
}
